import asyncio
import json
import time

###########################################
# 同项目调用基础包
import os
import sys
import time

filepath = os.path.abspath(__file__)
pathlist = filepath.split(os.sep)
pathlist = pathlist[:-5]
TopPath = os.sep.join(pathlist)
sys.path.insert(0, TopPath)
print(TopPath)
############################################

from re_common.baselibrary.utils.basemotor import BaseMotor
from re_common.baselibrary.utils.baserequest import BaseRequest

bs = BaseMotor()
bs.AsyncIOMotorClient(
    "mongodb://192.168.31.30:32417/htmljson.paper_jinjiribao?authSource=htmljson",
    "htmljson")

bs.get_col("paper_beijingribao")

bs2 = BaseMotor()
bs2.AsyncIOMotorClient(
    "mongodb://192.168.31.30:32417/htmljson.paper_jinjiribao?authSource=htmljson",
    "htmljson")

bs2.get_col("a_paper_latest")

import aiohttp
import asyncio

async def dic_hook(big_dict):

    url = 'http://127.0.0.1:8000/paper_beijingribao/parse_detail'
    new_dict = dict()
    print(big_dict['_id'])
    # new_dict['_id'] = big_dict['_id']
    new_dict['years'] = big_dict['years']
    new_dict['moths'] = big_dict['moths']
    new_dict['days'] = big_dict['days']
    new_dict['versions_title'] = big_dict['versions_title']
    for info in big_dict['jsondicts']:
        if info == 'count':
            continue
        new_dict['index'] = info
        new_dict['title'] = big_dict['jsondicts'][info]['title']
        new_dict['url'] = big_dict['jsondicts'][info]['url']
        new_dict['down_date'] = big_dict['jsondicts'][info]['down_date']
        new_dict['html'] = big_dict['jsondicts'][info]['html']

        info = json.dumps(new_dict)
        data = {
            "djson": info
        }

        async with aiohttp.ClientSession() as session:
            async with session.post(url, data=data) as r:

                djson = json.loads(await r.text())['data']
                djson['_id'] = djson['lngid_alt']
                # print(djson)
                try:
                    await  bs2.insert_one(djson)
                    print("{}插入成功".format(djson['lngid_alt']))
                except Exception as e:
                    # print(e)
                    # bs2.update({"_id": djson['lngid_alt']}, djson)
                    print("{}更新成功".format(djson['lngid_alt']))





asyncio.get_event_loop().run_until_complete(bs.find(dic_hook, {"stat" : 1}))
